<template>
  <div>
    <el-divider content-position="left">新建分类</el-divider>
    <el-form :rules="rules" ref="ruleForm" :model="form" label-width="80px">
      <el-form-item>
        <el-select v-model="form.parent" placeholder="请选择">
          <el-option v-for="item in options" :key="item._id" :label="item.name" :value="item._id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { categories, list } from '../api/module/categories'
export default {
  //import引入的组件
  components: {},
  //子组件引用父组件:data=data 方法@search=search使用this.emit('search', '参数');
  props: [],
  data () {
    return {
      form: {
        name: '',
        parent: ''
      },
      rules: {
        name: [
          // { required: true, message: '请输入分类名称', trigger: 'blur' },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
      },
      options: []
    };
  },
  methods: {
    onSubmit () {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          categories(this.form).then((result) => {
            console.log(result)
          })
          // alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  },
  mounted () {
    list().then((result) => {
      this.options = result
    })
  },
};
</script>

<style></style>